﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using NigiriClub.WebUI.Models;

namespace NigiriClub.WebUI.Controllers
{
    public class UploadFilesController : Controller
    {
        private FileListModel UplodFile(List<HttpPostedFileBase> fileList, string folder)
        {
            FileListModel files = new FileListModel();

            if (fileList == null)
            {
                return files;
            }

            foreach (var item in fileList)
            {
                string fileRelativePath = String.Format("/Content/{0}/{1}-{2}",
                    folder, Guid.NewGuid().ToString().Substring(0, 13), item.FileName);
                string filePath = Server.MapPath(fileRelativePath);

                item.SaveAs(filePath);
                files.FileNames.Add(fileRelativePath);
            }
            return files;
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadImage(List<HttpPostedFileBase> file)
        {
            return Json(UplodFile(file, "images"), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadAvatar(List<HttpPostedFileBase> file)
        {
            return Json(UplodFile(file, "images/UserPictures"), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadSgf(List<HttpPostedFileBase> file)
        {
            return Json(UplodFile(file, "games"), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult ImageJson(HttpPostedFileBase res)
        {
            List<HttpPostedFileBase> list = new List<HttpPostedFileBase>();
            list.Add((HttpPostedFileBase)res);
            return Json(UplodFile(list, "games"), JsonRequestBehavior.AllowGet);
        }

    }
}
